
#include <Wire.h>
#include <ESP8266WiFi.h>
#include "SoftwareSerial.h"
/*
   comprintf.c
*/
#include "comprintf.h"
/*
   comprintf.c
*/
#ifndef D5
#if defined(ESP8266)
#define D5 (14)
#define D6 (12)
#define D7 (13)
#define D8 (15)
#define TX (1)
#elif defined(ESP32)
#define D5 (18)
#define D6 (19)
#define D7 (23)
#define D8 (5)
#define TX (1)
#endif
#endif

#ifdef ESP32
#define BAUD_RATE 57600
#else
#define BAUD_RATE 57600
#endif
SoftwareSerial TJC;
void initTJCK028()
{
  TJC.begin(115200, SWSERIAL_8N1, D5, D6, false, 256);
  // high speed half duplex, turn off interrupts during tx
  TJC.enableIntTx(false);
}
/*
   comprintf.c
*/
void ScreenPrintf(const char *cmd, ...)
{
  uint8_t txt[50];				//文本数组,如果不够长可以自行改大,如果找不到uint8_t,可以用unsigned char代替
  uint8_t *txt_p;					//文本数组指针
  memset(&txt, 0, sizeof(txt));
  memset(&txt_p, 0, sizeof(txt_p));
  va_list args;       //定义一个va_list类型的变量，用来储存单个参数
  va_start(args, cmd); //使args指向可变参数的第一个参数
  vsprintf((char *)txt, (const char *)cmd, args);
  va_end(args);
  txt_p = txt;
  while (*txt_p != '\0')
  {
    TJC.write(*txt_p); //这个函数改为你的单片机的串口发送单字节函数
    txt_p++;
  }
  TJC.write(0xff);   //这个函数改为你的单片机的串口发送单字节函数
  TJC.write(0xff);   //这个函数改为你的单片机的串口发送单字节函数
  TJC.write(0xff);   //这个函数改为你的单片机的串口发送单字节函数
}
/*
   comprintf.c
*/
void TJCK028() {
  xianshi1();//显示ALS
  xianshi2();//PS
  xianshi4();//显示阈值
  if (a4 == 1)//  disable als
  {
    TJCliangdu(mn_sensor_als_data_lux);
  }
  //  String readdata = "";
  String readdata = "";
  while ( TJC.available() > 0 )
  {
    for (int i = 0; i < 5; i++) {
      readdata += char(TJC.read());
    }
    Serial.println(readdata[1]);
    if (readdata[1] == 0x02)//enable ALS
    {
      if (readdata[2] == 0x00)//  disable als
      { digitalWrite(LED_BUILTIN, HIGH);
        Serial.printf("[mn_grease_algr]: dis=%u\n", readdata[2]);
        b0 = 0;
        b1 = 1;
      } else if (readdata[2] == 0x01)//enable als
      {
        digitalWrite(LED_BUILTIN, LOW);
        Serial.printf("[mn_grease_algr]: dis=%u\n", readdata[2]);
        b0 = 1;
        b1 = 1;
      }

    } else if (readdata[1] == 0x03)//enable PS
    {
      if (readdata[2] == 0x00)//disable ps
      {
        a0 = 0;
        a1 = 1;
      } else if (readdata[2] == 0x01)//enable ps
      {
        a0 = 1;
        a1 = 1;
      }
    } else if (readdata[1] == 0x00)//ALS INT
    {
      if (readdata[2] == 0x00)//disable ALS  int
      {
        d0 = 0;
        d1 = 1;
      } else if (readdata[2] == 0x01)//enable ALS  int
      {
        d0 = 1;
        d1 = 1;
      }
    } else if (readdata[1] == 0x01)//PS INT
    {
      if (readdata[2] == 0x00)//disable pS  int
      {
        c0 = 0;
        c1 = 1;
      } else if (readdata[2] == 0x01)//enable PS  int
      {
        c0 = 1;
        c1 = 1;
      }
    }
    else if (readdata[1] == 0x05)//PS INT
    {
      if (readdata[2] == 0x00)//disable pS  int
      {
        a4 = 0;
      } else if (readdata[2] == 0x01)//enable PS  int
      {
        a4 = 1;
      }
    }
    else if (readdata[1] == 0x06)//reg打印
    {
      if (readdata[2] == 0x00)//关闭打印
      {
        a5 = 0;
      } else if (readdata[2] == 0x01)//打开打印
      {
        a5 = 1;
      }
    }
  }
}
void xianshi1()//显示ALS
{
  //ScreenPrintf("t0.txt=\"%d\"",a);
  //ScreenPrintf("n0.val=%d",b);
  ScreenPrintf("alsch0.txt=\"%d\"", mn_sensor_als_data_channel0);
  ScreenPrintf("alsch1.txt=\"%d\"", mn_sensor_als_data_channel1);
  ScreenPrintf("alsag.txt=\"%d\"", mn_sensor_als_ag_current_stage);
  ScreenPrintf("alslux.txt=\"%d\"", mn_sensor_als_data_lux);
}
void xianshi2()//显示PS
{
  ScreenPrintf("psir.txt=\"%d\"", mn_sensor_ps_data_ir_data);
  ScreenPrintf("psraw.txt=\"%d\"", mn_sensor_ps_data_pdata);
  ScreenPrintf("alsag.txt=\"%d\"", mn_sensor_als_ag_current_stage);
  ScreenPrintf("psstatus.txt=\"%d\"", mTask_data_prox_data);
}
void xianshi4()//显示PS
{
  ScreenPrintf("low.txt=\"%d\"", pilt);
  ScreenPrintf("high.txt=\"%d\"", piht);
  ScreenPrintf("min.txt=\"%d\"", mn_sensor_ps_dynk_min_ps_raw_data);
}
void TJCliangdu(int32_t value)
{
  if (value > 5000)
  {
    value = 5000;
  }
  liangdu = map(value, 0, 5000, 0, 100);
  Serial.printf("[mn_grease_algr]: liangdu=%u\n", liangdu);
  ScreenPrintf("h0.val=%d", liangdu);
  ScreenPrintf("n0.val=%d", liangdu);
  ScreenPrintf("dim=%d", liangdu);
}
